package com.ball.dev.service.impl;

import com.ball.common.dtos.ResponseResult;
import com.ball.common.enums.AppHttpCodeEnum;
import com.ball.common.utils.CollUtils;
import com.ball.dev.entity.po.Post;
import com.ball.dev.entity.po.PostLikes;
import com.ball.dev.mapper.PostLikesMapper;
import com.ball.dev.service.IPostLikesService;
import com.ball.dev.service.IPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lou
 * @since 2024-03-27
 */
@Service
@RequiredArgsConstructor
public class PostLikesServiceImpl extends ServiceImpl<PostLikesMapper, PostLikes> implements IPostLikesService {

    private final IPostService postService;
    /**
     * 添加或取消帖子点赞
     * @param postLikes 包含帖子ID、用户ID和状态（0为点赞，1为取消点赞）的对象
     * @return 根据操作结果返回的响应结果对象，成功返回带有消息的正常结果，失败返回带有错误代码和消息的结果
     */
    @Override
    public ResponseResult addPostLikes(PostLikes postLikes) {
        // 检查传入的PostLikes对象是否为null
        if (postLikes == null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"参数不能为空");
        }

        // 检查PostLikes对象中的关键参数是否合法
        if (postLikes.getPostId() == null &&  postLikes.getUserId() == null && postLikes.getStatus() ==null){
            return   ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数不合法");
        }

        // 处理点赞逻辑
        if (postLikes.getStatus() == 0){
            // 尝试根据用户ID和帖子ID查询已存在的点赞记录
            PostLikes postLikes1 = lambdaQuery().eq(PostLikes::getUserId, postLikes.getUserId())
                    .eq(PostLikes::getPostId, postLikes.getPostId())
                    .one();
            // 如果不存在，则插入新的点赞记录
            if (postLikes1 == null){
                postLikes.setCreateTime(LocalDateTime.now());
                postLikes.setUpdateTime(LocalDateTime.now());
                save(postLikes);
                postService.lambdaUpdate().eq(Post::getId, postLikes.getPostId())
                        .setSql("likes = likes + 1").update();


                return   ResponseResult.okResult("点赞成功");
            }
            // 如果存在，则更新为点赞状态
            postLikes1.setUpdateTime(LocalDateTime.now());
            postLikes1.setStatus(0);
            updateById(postLikes1);

            postService.lambdaUpdate().eq(Post::getId, postLikes.getPostId())
                    .setSql("likes = likes + 1").update();
            return   ResponseResult.okResult("点赞成功");
        }
        // 更新为取消点赞状态
       lambdaUpdate().eq(PostLikes::getPostId,postLikes.getPostId())
                       .eq(PostLikes::getUserId,postLikes.getUserId())
               .set(PostLikes::getStatus,1)
               .set(PostLikes::getUpdateTime,LocalDateTime.now())
               .update();

        postService.lambdaUpdate().eq(Post::getId, postLikes.getPostId())
                .setSql("likes = likes  - 1").update();
        return ResponseResult.okResult("取消点赞成功");
    }

}
